#include "myString.h"

boolean isSameBegin (myKata kataTemp) {
    myKata begin;
    int i;
    boolean cek = true;
    createEmpty(&begin);
    begin.length = 5;
    begin.kata[0] = 'b';
    begin.kata[1] = 'e';
    begin.kata[2] = 'g';
    begin.kata[3] = 'i';
    begin.kata[4] = 'n';

    i = 0;
    if (kataTemp.length == begin.length) {
        while ((i < begin.length) && cek) {
            if (begin.kata[i] != kataTemp.kata[i]) {
                cek = false;
            } else {
                i++;
            }
        }
    } else {
        cek = false;
    }
    return cek;
}

boolean isSameEnd (myKata kataTemp) {
    myKata end;
    int i = 0;
    boolean cek = true;
    createEmpty(&end);
    end.length = 3;
    end.kata[0] = 'e';
    end.kata[1] = 'n';
    end.kata[2] = 'd';

    if (kataTemp.length == end.length) {
        while ((i < end.length) && (cek)) {
            if (end.kata[i] != kataTemp.kata[i]) {
                cek = false;
            } else {
                i++;
            }
        }
    } else {
        cek = false;
    }
    return cek;
}

boolean isSameIf (myKata kataTemp){
    myKata ifs;
    int i = 0;
    boolean cek = true;
    createEmpty(&ifs);
    ifs.length = 2;
    ifs.kata[0] = 'i';
    ifs.kata[1] = 'f';

    if (kataTemp.length == ifs.length) {
        while ((i < ifs.length) && (cek)) {
            if (ifs.kata[i] != kataTemp.kata[i]) {
                cek = false;
            } else {
                i++;
            }
        }
    } else {
        cek = false;
    }
    return cek;
}

boolean isSameBuKur (myKata kataTemp) {
    if (kataTemp.kata[0] == '{') {
        return true;
    } else {
        return false;
    }
}

boolean isSameTuKur (myKata kataTemp){
    if (kataTemp.kata[(kataTemp.length)-1] == '}') {
        return true;
    } else {
        return false;
    }
}

boolean isSameThen (myKata kataTemp){
    myKata then;
    int i = 0;
    boolean cek = true;
    createEmpty(&then);
    then.length = 4;
    then.kata[0] = 't';
    then.kata[1] = 'h';
    then.kata[2] = 'e';
    then.kata[3] = 'n';

    if (kataTemp.length == then.length) {
        while ((i < then.length) && (cek)) {
            if (then.kata[i] != kataTemp.kata[i]) {
                cek = false;
            } else {
                i++;
            }
        }
    } else {
        cek = false;
    }
    return cek;
}

boolean isSameElse (myKata kataTemp){
    myKata elses;
    int i = 0;
    boolean cek = true;
    createEmpty(&elses);
    elses.length = 4;
    elses.kata[0] = 'e';
    elses.kata[1] = 'l';
    elses.kata[2] = 's';
    elses.kata[3] = 'e';

    if (kataTemp.length == elses.length) {
        while ((i < elses.length) && (cek)) {
            if (elses.kata[i] != kataTemp.kata[i]) {
                cek = false;
            } else {
                i++;
            }
        }
    } else {
        cek = false;
    }
    return cek;
}

boolean isSameWhile (myKata kataTemp) {
    myKata While;
    int i;
    boolean cek = true;
    createEmpty(&While);
    While.length = 5;
    While.kata[0] = 'w';
    While.kata[1] = 'h';
    While.kata[2] = 'i';
    While.kata[3] = 'l';
    While.kata[4] = 'e';

    i = 0;
    if (kataTemp.length == While.length) {
        while ((i < While.length) && cek) {
            if (While.kata[i] != kataTemp.kata[i]) {
                cek = false;
            } else {
                i++;
            }
        }
    } else {
        cek = false;
    }
    return cek;
}

boolean isSameDo (myKata kataTemp) {
    myKata Do;
    int i = 0;
    boolean cek = true;
    createEmpty(&Do);
    Do.length = 2;
    Do.kata[0] = 'd';
    Do.kata[1] = 'o';

    if (kataTemp.length == Do.length) {
        while ((i < Do.length) && (cek)) {
            if (Do.kata[i] != kataTemp.kata[i]) {
                cek = false;
            } else {
                i++;
            }
        }
    } else {
        cek = false;
    }
    return cek;
}

boolean isSameRepeat (myKata kataTemp){
    myKata Repeat;
    int i;
    boolean cek = true;
    createEmpty(&Repeat);
    Repeat.length = 6;
    Repeat.kata[0] = 'r';
    Repeat.kata[1] = 'e';
    Repeat.kata[2] = 'p';
    Repeat.kata[3] = 'e';
    Repeat.kata[4] = 'a';
    Repeat.kata[5] = 't';
    i = 0;
    if (kataTemp.length == Repeat.length) {
        while ((i < Repeat.length) && cek) {
            if (Repeat.kata[i] != kataTemp.kata[i]) {
                cek = false;
            } else {
                i++;
            }
        }
    } else {
        cek = false;
    }
    return cek;
}

boolean isSameUntil (myKata kataTemp){
    myKata Until;
    int i;
    boolean cek = true;
    createEmpty(&Until);
    Until.length = 5;
    Until.kata[0] = 'u';
    Until.kata[1] = 'n';
    Until.kata[2] = 't';
    Until.kata[3] = 'i';
    Until.kata[4] = 'l';

    i = 0;
    if (kataTemp.length == Until.length) {
        while ((i < Until.length) && cek) {
            if (Until.kata[i] != kataTemp.kata[i]) {
                cek = false;
            } else {
                i++;
            }
        }
    } else {
        cek = false;
    }
    return cek;
}

boolean isSameInput (myKata kataTemp) {
    myKata input;
    int i;
    boolean cek = true;
    createEmpty(&input);
    input.length = 5;
    input.kata[0] = 'i';
    input.kata[1] = 'n';
    input.kata[2] = 'p';
    input.kata[3] = 'u';
    input.kata[4] = 't';

    i = 0;
    if (kataTemp.length == input.length) {
        while ((i < input.length) && cek) {
            if (input.kata[i] != kataTemp.kata[i]) {
                cek = false;
            } else {
                i++;
            }
        }
    } else {
        cek = false;
    }
    return cek;
}

boolean isSameOutput (myKata kataTemp) {
    myKata output;
    int i;
    boolean cek = true;
    createEmpty(&output);
    output.length = 6;
    output.kata[0] = 'o';
    output.kata[1] = 'u';
    output.kata[2] = 't';
    output.kata[3] = 'p';
    output.kata[4] = 'u';
    output.kata[5] = 't';

    i = 0;
    if (kataTemp.length == output.length) {
        while ((i < output.length) && cek) {
            if (output.kata[i] != kataTemp.kata[i]) {
                cek = false;
            } else {
                i++;
            }
        }
    } else {
        cek = false;
    }
    return cek;
}

boolean isSameOperasi (myKata kataTemp) {
    int i = 0;
    boolean found = false;
    while (i < kataTemp.length) {
        if (IsOperator(kataTemp,i)) {
            found = true;
        }
        i++;
    }
    return found;
}

boolean isSameKomentar (myKata kataTemp);

boolean isSameCondition (myKata kataTemp) {
    int i = 0;
    boolean found = false;
    while (i < kataTemp.length) {
        if (IsPembanding(kataTemp,i)) {
            found = true;
        }
        i++;
    }
    return found;
}

void createEmpty (myKata * word) {
    int i;
    (*word).kata = (char *) malloc (101);
    for (i = 0; i < 101; i++) {
        (*word).kata[i] = '\0';
    }
    (*word).length = 0;
}

boolean isNull(myKata kataTemp) {
    if (kataTemp.length == 0) {
        return true;
    } else {
        return false;
    }
}

myKata getSubString (myKata temp, int start, int end) {
    myKata dum;
    createEmpty(&dum);
    int i = 0;
    int j = start - 1;
    while (j < end) {
        dum.kata[i]=temp.kata[j];
        i++;
        j++;
    }
    dum.length = end - start + 1;
    return dum;
}

boolean IsPembanding (myKata temp,int i) {
    if ((temp.kata[i] == '<') || (temp.kata[i] == '=') || (temp.kata[i] == '>')) {
        return true;
    } else {
        return false;
    }
}

boolean IsSamaDengan (myKata kataTemp) {
    int i = 0;
    boolean found = false;
    while (i < kataTemp.length) {
        if (IsOperator(kataTemp,i)) {
            found = true;
        }
        i++;
    }
    return found;
}

boolean IsAngka (myKata temp, int i) {
    if ((temp.kata[i] >= '0') && (temp.kata[i] <= '9')) {
        return true;
    } else {
        return false;
    }
}

boolean IsOperator (myKata temp, int i) {
    if ((temp.kata[i] == '+') || (temp.kata[i] == '-') || (temp.kata[i] == '*') || (temp.kata[i] == '/')) {
        return true;
    } else {
        return false;
    }
}

boolean IsVariabel (myKata temp) {
    int i;
    i = 0;
    boolean found = true;
    toLower(&temp);
    if (IsAngka(temp,i)) {
        i = 1;
        while ((i < temp.length) && (found)) {
            if (IsAngka(temp,i)) {
                i++;
            } else {
                found = false;
            }
        }
    } else if (isSameBegin(temp) || isSameEnd(temp) || isSameWhile(temp) || isSameDo (temp) ||
               isSameRepeat(temp) || isSameUntil(temp) || isSameIf(temp) || isSameThen (temp) ||
               isSameElse(temp) || isSameInput(temp) || isSameOutput(temp)) {
        found = false;
    }
    return found;
}

boolean IsVar(myKata temp) {
    int i;
    i = 0;
    boolean found = true;
    toLower(&temp);
    if (IsAngka(temp,i)) {
        found = false;
    } else if (isSameBegin(temp) || isSameEnd(temp) || isSameWhile(temp) || isSameDo (temp) ||
               isSameRepeat(temp) || isSameUntil(temp) || isSameIf(temp) || isSameThen (temp) ||
               isSameElse(temp) || isSameInput(temp) || isSameOutput(temp)) {
        found = false;
    }
    return found;
}

void toLower (myKata * kataTemp) {
    int i;

    for (i = 0; i < (*kataTemp).length;i++) {
        if (IsHurufBesar((*kataTemp),i)) {
            (*kataTemp).kata[i] += 32;
        }
    }
}

boolean IsHurufBesar(myKata kata, int i) {
    if ((kata.kata[i] >= 'A') && (kata.kata[i] <= 'Z')) {
        return true;
    } else {
        return false;
    }
}

boolean IsOperasi (myKata kataTemp) {
    int i;
    i = 0;
    boolean found = false;
    while (i < kataTemp.length && !found) {
        if (kataTemp.kata[i] == '=') {
            found = true;
        } else {
            i++;
        }
    }
    return found;
}
